package com.doyled.jump.object;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import com.doyled.jump.IGame.GameInterface;
import com.doyled.jump.animation.DiceAnimation;
import com.doyled.jump.framework.ResourceManager;
import com.doyled.jump.util.Drawing;

public class Dice implements GameInterface {
	private final int DICE_EDGE = 149;
	private int x = 360, y = 360;
	private Random random;
	private int value;
	private BufferedImage diceImage;
	private DiceAnimation animation;
	
	

	public Dice() {
		diceImage = ResourceManager.getInstance().getDiceImage();
		random = new Random();
		animation = new DiceAnimation();
	}

	/**
	 * get value of dice
	 * @return
	 */
	public int getValue() {
		return value;
	}

	public DiceAnimation getAnimation() {
		return animation;
	}

	/**
	 * Throw the dice
	 */
	public void throwDice() {
		if (!animation.isAnimating()) {
			value = random.nextInt(6) + 1;
			animation.setAnimation(1, value);
		}
	}

	/**
	 * the point is on Dice or not
	 * @param x
	 * @param y
	 * @return	true or false
	 */
	public boolean isInDice(int x, int y) {
		int halfW = DICE_EDGE / 2;
		int halfH = DICE_EDGE / 2;
		return (x >= this.x - halfW && x <= this.x + halfW)
				&& (y >= this.y - halfH && y <= this.y + halfH);
	}

	@Override
	public void tick() {
	}

	/**
	 * get subImage of the dice
	 * @param diceNo	dice number
	 * @return
	 */
	private BufferedImage getSubImage(int diceNo) {
		--diceNo;
		return diceImage.getSubimage(diceNo * DICE_EDGE, 0, DICE_EDGE,
				DICE_EDGE);
	}

	@Override
	public void render(Graphics g) {
		BufferedImage specific = getSubImage(animation.getValue());
		Drawing.draw(g, specific, x, y, 50, 50);
	}

}
